<template>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
        <el-form :inline="true" :disabled="disabled">

            <el-form-item label="一级分类">
                <el-select placeholder="请选择" v-model="category1Id" @change="changeSelect1">
                    <el-option 
                        v-for="item in category1List"
                        :key="item.id"
                        :label="item.name" 
                        :value="item.id" 
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="二级分类">
                <el-select placeholder="请选择" v-model="category2Id" @change="changeSelect2">
                    <el-option 
                        v-for="item in category2List"
                        :key="item.id"
                        :label="item.name" 
                        :value="item.id" 
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="三级分类">
                <el-select placeholder="请选择" v-model="category3Id" >
                    <el-option 
                        v-for="item in category3List"
                        :key="item.id"
                        :label="item.name" 
                        :value="item.id" 
                    />
                </el-select>
            </el-form-item>
      
        </el-form>
    </el-card>
    
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
    name: "Category",
});
</script>

<script setup lang="ts">
import useAttrStore from "@/stores/category";  
import { storeToRefs } from "pinia";
import {onMounted} from "vue"

defineProps<{
    disabled: boolean
}>()

//拿到仓库实例
const atrrStore = useAttrStore()
//解构
const {
    category1List,
    category2List,
    category3List,
    category1Id,
    category2Id,
    category3Id,
} = storeToRefs(atrrStore)

//1.初始化的时候请求一级列表
onMounted(() => {
    atrrStore.getCategory1List()
});

//2.改变一级列表的回调
const changeSelect1 = () => {
    //点击一级列表先清空二级三级列表的数据
    category3List.value = []
    category2Id.value = null
    category3Id.value = null
    //改变一级列表，小菠萝中的category1Id就会收到value中保存的category1Id的值，
    //然后发送二级列表请求
    atrrStore.getCategory2List()
}

//3.改变二级列表的回调
const changeSelect2 = () => {
    //点击一级列表先清空三级列表的数据
    category3Id.value = null
    //改变一级列表，小菠萝中的category2Id就会收到value中保存的category2Id的值，
    //然后发送三级列表请求
    atrrStore.getCategory3List()
}

</script>